package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.QueryApplexConrelListReq;
import com.sg.entity.OmsConfApplexRel;
import com.sg.service.base.component.MOmsConfApplexRelService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConfApplexRelMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfApplexRelServiceImpl
        extends ServiceImpl<OmsConfApplexRelMapper, OmsConfApplexRel>
        implements MOmsConfApplexRelService {

    @Autowired
    OmsConfApplexRelMapper omsConfApplexRelMapper;

    /**
     * 2-3-13查询应用实例配置关系详情[2122]
     * gen by moon at 4/22/2023, 9:05:34 PM
     */
    @Trace(operationName = "2-3-13查询应用实例配置关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfApplexRel queryApplexConrelDetail(QueryApplexConrelDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsConfApplexRel> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfApplexRel> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfApplexRel retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-13新增应用实例配置关系[2120]
     * gen by moon at 10/16/2022, 11:04:31 AM
     */
    @Trace(operationName = "2-3-13新增应用实例配置关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addApplexConrel(OmsConfApplexRel omsConfApplexRel) {
        omsConfApplexRel.setApplexRelId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfApplexRel.setIsArchive("FALSE");
        omsConfApplexRel.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfApplexRel.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfApplexRel != null && omsConfApplexRel.getSubjectLifeCycle() != null && omsConfApplexRel.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfApplexRel.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfApplexRel.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfApplexRel.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfApplexRel != null && omsConfApplexRel.getSubjectLifeCycle() != null && omsConfApplexRel.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfApplexRel.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsConfApplexRel.setIsValid("FALSE");
        omsConfApplexRel.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfApplexRel.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfApplexRel)) {
            return omsConfApplexRel.getApplexRelId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-13修改应用实例配置关系[2374]
     * gen by moon at 10/16/2022, 11:04:34 AM
     */
    @Trace(operationName = "2-3-13修改应用实例配置关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateApplexConrel(OmsConfApplexRel omsConfApplexRel) {
        omsConfApplexRel.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfApplexRel != null && omsConfApplexRel.getSubjectLifeCycle() != null && omsConfApplexRel.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfApplexRel.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsConfApplexRel != null && omsConfApplexRel.getSubjectLifeCycle() != null && omsConfApplexRel.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfApplexRel.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsConfApplexRel.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfApplexRel != null && omsConfApplexRel.getSubjectLifeCycle() != null && omsConfApplexRel.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfApplexRel.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsConfApplexRel != null && omsConfApplexRel.getSubjectLifeCycle() != null && omsConfApplexRel.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfApplexRel.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsConfApplexRel);
    }

    /**
     * 2-3-13删除应用实例配置关系[2373]
     * gen by moon at 10/16/2022, 11:04:34 AM
     */
    @Trace(operationName = "2-3-13删除应用实例配置关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteApplexConrel(String applexRelId) {

        return super.removeById(applexRelId);
    }

    /**
     * 2-3-13批量修改应用实例配置关系[2132]
     * gen by moon at 10/16/2022, 11:04:33 AM
     */
    @Trace(operationName = "2-3-13批量修改应用实例配置关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateApplexConrel(List<OmsConfApplexRel> omsConfApplexRel) {

        if (omsConfApplexRel == null || omsConfApplexRel.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsConfApplexRel);

    }

    /**
     * 2-3-13查询应用实例配置关系列表[2124]
     * gen by moon at 10/16/2022, 11:04:33 AM
     */
    @Trace(operationName = "2-3-13查询应用实例配置关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfApplexRel> queryApplexConrelList(QueryApplexConrelListReq reqDto) {

        try {
            QueryWrapper<OmsConfApplexRel> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfApplexRel.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfApplexRel.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfApplexRel.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfApplexRel>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-13批量删应用实例配置关系[4198]
     * gen by moon at 10/16/2022, 11:04:35 AM
     */
    @Trace(operationName = "2-3-13批量删应用实例配置关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteApplexConrel(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M2查询应用实例配置关系测试列表[4891]
     * gen by moon at 10/16/2022, 11:04:36 AM
     */
    @Trace(operationName = "M2查询应用实例配置关系测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfApplexRel> queryConfApplexRelDataList(OmsConfApplexRel omsConfApplexRel) {

        try {
            QueryWrapper<OmsConfApplexRel> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsConfApplexRel);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfApplexRel.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfApplexRel.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfApplexRel.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfApplexRel>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M2批量删应用配置实例关系测试[4892]
     * gen by moon at 10/16/2022, 11:04:36 AM
     */
    @Trace(operationName = "M2批量删应用配置实例关系测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteConfApplexRelData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
